package org.colcon.web.client.ui.deprecated;

import org.colcon.web.client.Collective_Congress;
import org.colcon.web.model.Ballot;
import org.colcon.web.model.framework.IUser;
import org.colcon.web.model.framework.IVote;
import org.colcon.web.model.vote.ElectionVote;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ElectionPanel extends InfoPanel 
{
	private VerticalPanel panel;
	private RadioButton[] radioButtons;
	private IUser[] candidates;
	
	public ElectionPanel(Collective_Congress client, IVote vote) 
	{
		super(client, vote);
	}
	
	public void init()
	{
		super.init();
		
		IVote vote = getVote();
		
		sinkEvents(Event.ONKEYPRESS);
		addHandler(new MyEnterHandler(), KeyPressEvent.getType());
		
		panel = new VerticalPanel();
		
		if(!vote.isEligible(getClient().getUser()))
		{
			panel.clear();
			panel.add(new Label("You are not eligible for this vote or have already voted"));
			add(panel);
			return;
		}
		
		ElectionVote election = (ElectionVote) vote;
		
		candidates = election.getCandidates();
		radioButtons = new RadioButton[candidates.length];
		
		for(int i = 0; i < candidates.length; i++)
		{
			String username = candidates[i].getId();
			int votes = election.getVotes(username);
			if(votes > 0)
				add(new Label(username + ": " + votes));
		}
		
		for(int i = 0; i < candidates.length; i++)
		{
			radioButtons[i] = new RadioButton("buttonGroup", candidates[i].getId());
			panel.add(radioButtons[i]);
		}
		
		Button button = new Button("Submit Vote");
		button.addClickHandler(new MyButtonHandler());
		panel.add(button);
		
		add(panel);
	}

	public IVote getVote()
	{
		return (IVote) getItem();
	}
	
	private class MyButtonHandler implements ClickHandler
	{
		public void onClick(ClickEvent event) 
		{
			buttonClick();
		}
	}
	
	private class MyEnterHandler implements KeyPressHandler
	{
		public void onKeyPress(KeyPressEvent event) 
		{
			if(event.getCharCode() == KeyCodes.KEY_ENTER)
				buttonClick();
		}
	}
	
	public void buttonClick()
	{
		String user = getClient().getUser().getId();
		
		if(getVoteValue() == null)
		{
			init();
			panel.add(new HTML("Invalid vote value"));
			return;
		}
		
		getService().submitVote(getVote().getPath(), user, getVoteValue(), new AsyncCallback<Ballot>()
		{
			public void onFailure(Throwable caught)
			{
				panel.clear();
				caught.printStackTrace();
				panel.add(new HTML(caught.getMessage()));
			}
			
			public void onSuccess(Ballot ballot)
			{
				if(ballot != null)
				{
					panel.clear();
					panel.add(new HTML("Vote successful"));
				}
				else
				{
					panel.clear();
					panel.add(new HTML("Vote failed"));
				}
			}
		});
	}
	
	public String getVoteValue()
	{
		for(int i = 0; i < radioButtons.length; i++)
			if(radioButtons[i].getValue())
				return candidates[i].getId();
		
		return null;
	}
}
